<#include "/common/_header.html" />
<div class="page-group"><!-- 页面组 -->
	<div class="page page-current"><!-- 页面组中的每一个页面，可以互相跳转 -->
		<header class="bar bar-nav"><!-- 每个页面的头部 -->
			<button class="button button-link button-nav pull-left" onclick="goBackword()">
				<span class="icon icon-left"></span> 返回
			</button>
			<h1 class="title" style="font-size: 1rem;">我的定位</h1>
		</header>
		<div class="content native-scroll"><!-- 每个页面的内容区域 -->
			<div id="l-map"></div>
			<div class="list-block">
				<ul id="r-result"></ul>
			</div>
		</div>
	</div>
</div>
<#include "/common/_footer.html" />
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=${baiduAK}"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>
	$.showPreloader('定位中，请稍后...');
	wx.config({
	    debug: false,
	    appId: '${appId}',
	    timestamp: ${timestamp},
	    nonceStr: '${noncestr}',
	    signature: '${signature}',
	    jsApiList: [
	      'checkJsApi',
	      'openLocation',
	      'getLocation',
	      'closeWindow'
	    ]
	});
	wx.ready(function(){
		wx.checkJsApi({
            jsApiList: [
				'checkJsApi',
				'openLocation',
				'getLocation',
				'closeWindow'
            ],
            success: function (res) {
            }
        });
		wx.getLocation({
		    type: 'wgs84', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
		    success: function (res) {
		        var longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
		        var latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
		        var speed = res.speed; // 速度，以米/每秒计
		        var accuracy = res.accuracy; // 位置精度
		    	//console.log(longitude+","+latitude);
		    	$.ajax({
		    		type:'post',
		    		url:'${path}/checkin/toBaiduMap',
		    		data:{"longitude":longitude,"latitude":latitude},
		    		success:function(data){
		    			var dataJson = JSON.parse(data);
		    			var lng = dataJson.result[0].x;
		    			var lat = dataJson.result[1].y;
				    	//珠江新城：113.328312,23.130024，黄埔花园：113.463779,23.104069
				    	//map.centerAndZoom(new BMap.Point(113.160484,23.060165), 11);
		    			// 百度地图API功能
				    	var map = new BMap.Map("l-map");            // 创建Map实例
				    	map.centerAndZoom(new BMap.Point(lng,lat), 11);
				    	var options = {
				    			onSearchComplete: function(results){
				    				// 判断状态是否正确
				    				if (local.getStatus() == BMAP_STATUS_SUCCESS){
				    					var s = [];
				    					for (var i = 0; i < results.getCurrentNumPois(); i ++){
				    						s.push("<li><a href='#' class='item-link item-content'><div class='item-inner'><div class='item-title'>"+results.getPoi(i).title + ", " + results.getPoi(i).address+"</div></div></a></li>");
				    					}
				    					document.getElementById("r-result").innerHTML = s.join("");
				    					$.hidePreloader();
				    					$("#r-result>li>a").click(function(){
				    						$("#r-result>li>a").each(function(){
				    							$(this).removeAttr("style");
				    						});
				    						$(this).css("background-color","#F9F589");
				    						var dist = $(".item-inner .item-title", $(this)).text();
				    						var dists = dist.split(", ");
				    						//限定只能在一个小区打卡，兼容有小区别名的情况
				    						if(dists[0]=='${uservo.userCommunityName}' || '${uservo.communityNameAlias!""}'.indexOf('##'+dists[0]+'##')>-1){
					    						$.alert('确认在【'+dists[0]+'】打卡？', function () {
					    							$.ajax({
					    					    		type:'post',
					    					    		url:'${path}/checkin/doCheckin',
					    					    		data:{"commName":dists[0]/* ,"commAddress":dists[1] */,"lng":lng,"lat":lat},
					    					    		success:function(data){
					    					    			if(data=="OK"){
					    					    				$.toast('打卡成功，正在跳转...');
					    					    				if('${fromAddBbs!0}'=='1'){
					    					    					location.href = "${path}/communityChat/add";
					    					    				}else{
					    					    					location.href = "${path}/checkin";
					    					    				}
					    					    			}else{
					    					    				$.alert('操作失败，请确认允许读取位置信息。');
					    					    			}
					    					    		}
					    							});
					    				        });				    							
				    						}else{
				    							$.alert('你家的小区在【${uservo.userCommunityName}】，请在【${uservo.userCommunityName}】打卡。');
				    						}
				    					});
				    				}
				    			}
				    		};
				    	var local = new BMap.LocalSearch(map, options);
				    	local.searchNearby("小区",new BMap.Point(lng,lat),300);
		    		},
		    		error:function(xhr,errorType , error) {
		    			console.log("sorry error\r\n");
		    		}
		    	});
		    }
		});
	});
	function goBackword(){
		if('${fromAddBbs!0}'=='1'){
			location.href = "${path}/communityChat";
		}else{
			location.href = "${path}/checkin";
		}
	}
</script>